package com.erb.controller.llb;

import com.erb.constants.AdminProductConstants;
import com.erb.constants.InvestConstants;
import com.erb.entity.*;
import com.erb.service.llb.IOtherService;
import com.erb.service.llb.InvestService;
import com.erb.service.zwj.IProductService;
import com.erb.utils.BigDecimalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 投资
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/base/invest")
public class InvestController {

	int countPass = 0;

	@Autowired
	InvestService investService;
	@Autowired
	IOtherService otherService;
	@Autowired
	StringRedisTemplate redisTemplate;
	@Resource
	private IProductService productService;

	/**
	 * 获取用户信息
	 *
	 * @param session
	 * @return
	 */
	@GetMapping("/user")
	public RespBean user(HttpSession session) {
		User user = (User) session.getAttribute("user");
		if (user != null) {
			User newUser = otherService.findUserById(user.getId());
			//更新用户session
			session.setAttribute("user", newUser);
			newUser.setPassword("*****");
			newUser.setRealName("*****");
			newUser.setPhone("******");
			newUser.setBankCard("******");
			return RespBean.ok("查询成功", newUser);
		} else {
			return RespBean.error("用户未登陆");
		}
	}

	/**
	 * 添加投资
	 *
	 * @param invest
	 * @return
	 */
	@PostMapping("/")
	public RespBean add(@RequestBody Invest invest, HttpSession session) {
		User currUser = (User) session.getAttribute("user");
		if (currUser == null) {
			return RespBean.error("未登陆用户");
		}
		if (currUser.getId() != invest.getUid()) {
			return RespBean.error("购买失败，请求错误");
		}
		User user = otherService.findUserById(invest.getUid());
		if (user.getBalance() < invest.getInvestMoney()) {
			return RespBean.error("购买失败，用户余额不足");
		}
		Product product = productService.findProductById(invest.getPid());
		if (product.getTotal() - product.getHaveMoney() < 0) {
			return RespBean.error("购买失败，该项目已满标");
		}
		if (product.getType() == AdminProductConstants.PRODUCT_TYPE_NEW) {
			int countInvest = otherService.countInvest(invest.getUid());
			if (countInvest >= 3) {
				return RespBean.error("购买失败，当前用户不满足购买条件");
			}
		}
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, invest.getLockDate());
		invest.setInvestDate(new Timestamp(System.currentTimeMillis()));
		invest.setEndDate(new Timestamp(calendar.getTimeInMillis()));

		Double profit = invest.getInvestMoney() * product.getRate() / 12 * product.getLockDateMonth();

		//四舍五入保留两位小数
		invest.setProfit(BigDecimalUtil.formatDouble(profit));
		invest.setInvsetStatus(1);
		//招标中
		invest.setStatus(InvestConstants.INVEST_STATUS_INBIDDING);
		if (investService.addInvest(invest) > 0) {
			return RespBean.ok("添加成功");
		} else {
			return RespBean.error("添加失败");
		}
	}

	/**
	 * 验证支付密码
	 *
	 * @param uid
	 * @param pass
	 * @return
	 */
	@GetMapping("/checkPayPass")
	public RespBean checkPayPass(int uid, int pass) {
		if (redisTemplate.opsForValue().get(uid + "_count") != null) {
			return RespBean.ok("输入错误次数过多,请30分钟后再试！", false);
		}
		int real = otherService.findPayPassByUid(uid);
		if (pass == real) {
			return RespBean.ok("密码正确", true);
		} else {
			countPass++;
			if (countPass >= 3) {
				countPass = 0;
				redisTemplate.opsForValue().set(uid + "_count", "erb");
				redisTemplate.expire(uid + "_count", 30, TimeUnit.MINUTES);
			}
			return RespBean.ok("密码错误", false);
		}
	}

	/**
	 * 获取用户红包
	 *
	 * @param uid
	 * @param rtype
	 * @param ptype
	 * @param state
	 * @return
	 */
	@GetMapping("/getRedpacket")
	public RespBean getRedpacket(int uid, Integer rtype, Integer ptype, Integer state) {
		List<Redpacketrecord> redByUser = otherService.findRedByUser(uid, rtype, ptype, state);
		if (redByUser.size() > 0) {
			return RespBean.ok("查询成功", redByUser);
		} else {
			return RespBean.ok("查询成功", Collections.emptyList());
		}
	}
}
